package com.yang.flower.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.flower.common.Result;
import com.yang.flower.entity.Flower;
import com.yang.flower.entity.Setmeal;
import com.yang.flower.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description: 套餐控制器
 * @Author: Yang
 * @DateTime: 2022/12/6 22:52
 **/
@RestController
@CrossOrigin
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //分页查询套餐信息
    @GetMapping("/page")
    public Page<Setmeal> page(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String name,
                              @RequestParam String code){
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Setmeal::getName,name).like(Setmeal::getCode,code);
        queryWrapper.orderByDesc(Setmeal::getCreateTime);
        Page<Setmeal> pageInfo = setmealService.page(page, queryWrapper);
        return pageInfo;
    }

    //添加套餐
    @PostMapping("/save")
    public Result save(@RequestBody Setmeal setmeal){
        setmeal.setCreateUser("user");
        return Result.success(setmealService.save(setmeal));
    }

    //修改套餐信息
    @PutMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal){
        return Result.success(setmealService.saveOrUpdate(setmeal));
    }

    //根据id删除套餐
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        return Result.success(setmealService.removeById(id));
    }

    //批量删除套餐
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<String> ids){
        return Result.success(setmealService.removeByIds(ids));
    }

    //修改套餐售卖状态
    @PutMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Integer id){
        Setmeal setmeal = setmealService.getById(id);
        String status = setmeal.getStatus();
        String newStatus = status.equals("起售") ? "停售" : "起售";
        setmeal.setStatus(newStatus);
        return Result.success(setmealService.saveOrUpdate(setmeal));
    }

    //导出
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<Setmeal> list = setmealService.list();
        // 通过工具类创建writer 写出到磁盘路径
        // ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("id", "ID");
        writer.addHeaderAlias("code", "套餐编码");
        writer.addHeaderAlias("name", "套餐名称");
        writer.addHeaderAlias("price", "套餐价格");
        writer.addHeaderAlias("status", "套餐销售状态");
        writer.addHeaderAlias("description", "描述");
        writer.addHeaderAlias("createTime", "创建时间");
        writer.addHeaderAlias("createUser", "创建人");

        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }


}
